const app = getApp()
const WXAPI = require("../../../../api/api")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nvabarData: {
      showCapsule: 1, // 是否显示左上角图标   1表示显示    0表示不显示
      title: '电子合约签署', // 导航栏 中间的标题
      textColor: '#333333', // 标题颜色
      bgColor: '#ffffff', // 导航栏背景颜色
      isColor: true,
      isloading: false,
    },
    signType: 1, // 1:未签约 2：已签约
    info: {},
    contract: '', //合同
    signUrl: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.data.info = JSON.parse(options.info)
    // this.getContract()
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let signInfo = wx.getStorageSync('signInfo')
    console.log('signInfo+++', signInfo)
    if (signInfo != '') {
      this.setData({
        signType: 2,
        contract: signInfo.img,
        signUrl: signInfo.url
      })
    } else if (this.data.contract == '') {
      this.getContract()
    }
  },
  //获取合同内容
  getContract() {
    var data = {
      assetId: this.data.info.assetId,
      merchantId: this.data.info.merchantId
    }
    wx.showLoading({
      title: '合同内容获取中',
      mask: true
    })
    WXAPI.signContractInfo(data, (res) => {
      this.setData({
        contract: 'data:image/png;base64,' + res.result
      })
      wx.hideLoading()
    })
    setTimeout(() => {
      wx.hideLoading()
    }, 10000)
  },
  //打开签名弹窗
  signView() {
    // this.setData({
    //   isShow: true
    // })
    wx.navigateTo({
      url: './signature/signature?assetId=' + this.data.info.assetId + '&merchantId=' + this.data.info.merchantId,
    })
  },
  wxpay() { //微信支付
    var that = this
    let data = {
      signUrl: this.data.signUrl,
      ...this.data.info
    }
    console.log('参数',data)
    WXAPI.createBuyOrder(data, (res) => {
      console.log('提交支付', res)
      if (res.code == 200) {
        var data = res.result
        wx.requestPayment({
          'timeStamp': data.timeStamp,
          'nonceStr': data.nonceStr,
          'package': data.package,
          'signType': data.signType, //MD5
          'paySign': data.paySign,
          'success': function (res) {
            console.log('支付成功', res)
            console.log(res)
            if (res.errMsg == 'requestPayment:ok') {
              wx.showToast({
                title: '支付成功',
              })
              that.setData({
                orderId: data.orderId
              })
              that.getCardPayorder()
              // setTimeout(() => {
              //   //支付页面信息
              //   wx.redirectTo({
              //     url: '/userPageB/pages/userStore/resellScuuess/resellScuuess?orderId='+data.orderId+"&pathType=cardPay",
              //   })
              // }, 1500);
            }
          },
          'fail': function (res) {
            wx.showToast({
              title: '支付失败',
              icon: 'none'
            })
            console.log('支付失败或取消支付', res)
          },
          'complete': function (res) {
            console.log('支付完成', res)
            // wx.navigateBack({})
          }
        })
      } else {
        if (res.message == '保额不足') {
          this.showModal()
        } else {
          wx.showToast({
            title: res.message,
            icon: 'none'
          })
        }
      }
    })
  },
  //购卡订单支付结果
  getCardPayorder() {
    const that = this
    this.setData({
      timer: setInterval(function () { //这里把setInterval赋值给变量名为timer的变量
        that.data.countdown--;
        var data = {
          orderId: that.data.orderId,
        }
        WXAPI.getBuyCardPayResult(data, (res) => {
          const data = res.result
          console.log("获取支付结果", res)
          if (data.isPay) {
            // wx.redirectTo({
            //   url: '/userPageB/pages/userStore/resellScuuess/resellScuuess?orderId=' + that.data.orderId + "&pathType=cardPay",
            // })
            wx.reLaunch({
              url: '/userPageB/pages/buyCardpuch/buyCardpuch?customerCardId=' + data.customerCardId,
            })
            clearInterval(that.data.timer);
          }
        })
        that.setData({
          countdown: that.data.countdown,
        })
        if (that.data.countdown == 0) {
          console.log("1111111111")
          clearInterval(that.data.timer);
          that.setData({
            countdown: 60,
          })
        }
      }, 1000)
    })
  },
  showModal() { //保额不足时弹窗
    var that = this
    this.data.info.isInsurance = '2'
    wx.showModal({
      title: '保额不足',
      content: '非常抱歉，商户剩余保额不足以承保本次支付金额，是否继续购买/充值。',
      confirmText: '继续购买',
      confirmColor: '#FF5800',
      success(res) {
        if (res.confirm) {
          console.log('用户点击确定')
          that.wxpay()
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage() {

  // }
})